﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MDIDemo.Windows;

namespace MDIDemo.Modules
{
    public partial class ThuChiHoaDon : DevExpress.XtraEditors.XtraForm
    {
        long no = -1;
        string sobt = "";
        string sErr = "";
        int idThem = 53;
        int idSua = 54;
        int idXoa = 55;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                btnXoa.Visible = frmMain.IsPermis(idXoa, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION) || frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
            }
        }
        public ThuChiHoaDon()
        {
            InitializeComponent();

        }

        private void btnTK1_Click(object sender, EventArgs e)
        {
            frmViewTaiKhoan frm = new frmViewTaiKhoan();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtTK1.Text = frm.ReturnRow[DTO.ACC_SYSInfo.Field.TKC2.ToString()].ToString();
            }
        }

        private void btnTK2_Click(object sender, EventArgs e)
        {
            frmViewTaiKhoan frm = new frmViewTaiKhoan();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtTK2.Text = frm.ReturnRow[DTO.ACC_SYSInfo.Field.TKC2.ToString()].ToString();
            }
        }

        private void btnTK3_Click(object sender, EventArgs e)
        {
            frmViewTaiKhoan frm = new frmViewTaiKhoan();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtTK3.Text = frm.ReturnRow[DTO.ACC_SYSInfo.Field.TKC2.ToString()].ToString();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            RefreshForm("");
            EnabledForm(true);
        }
        private string GetLoadBT()
        {
            if (rLoai.EditValue.ToString() == "GTGT" && rChungTu.EditValue.ToString() == "IN")
                return "T-GTGT";
            if (rLoai.EditValue.ToString() == "GTGT")
                return "C-GTGT";
            if (rChungTu.EditValue.ToString() == "IN")
                return "TM-THU";
            return "TM-CHI";
        }
        private void RefreshForm(string p)
        {
            sobt = "";
            no = -1;
            txtDiaChi.Text = p;
            txtDoanhSo.EditValue = 0;
            txtKyHieuHD.Text = p;
            txtMaSoThue.Text = p;
            txtNgayGD.EditValue = frmMain.__ngayMoSo;
            txtNgayPS.EditValue = frmMain.__ngayMoSo;
            txtNoiDung.Text = "";
            txtSOBT.Text = NXHoaDon.GetNewSoBT();
            txtSoHD.Text = p;
            txtTen.Text = p;
            txtThueGTGT.EditValue = 0;
            string type = GetLoadBT();
            if (type == "T-GTGT")
            {
                lbTK1.Text = "TK Nợ";
                lbTK2.Text = lbTK3.Text = "TK Có";
                txtTK1.Text = "1111";
                txtTK2.Text = "";
                txtTK3.Text = "33311";
            }
            else if (type == "C-GTGT")
            {
                lbTK1.Text = lbTK2.Text = "TK Nợ";
                lbTK3.Text = "TK Có";
                txtTK1.Text = "";
                txtTK2.Text = "1331";
                txtTK3.Text = "1111";
            }
            else if (type == "TM-THU")
            {
                lbTK1.Text = "TK Nợ";
                lbTK2.Text = lbTK3.Text = "TK Có";
                txtTK1.Text = "1111";
                txtTK2.Text = "";
                txtTK3.Text = "";

            }
            else
            {
                lbTK1.Text = "TK Nợ";
                lbTK2.Text = lbTK3.Text = "TK Có";
                txtTK1.Text = "";
                txtTK2.Text = "1111";
                txtTK3.Text = "";
            }

            txtTKValue1.EditValue = 0;
            txtTKValue2.EditValue = 0;
            txtTKValue3.EditValue = 0;
        }

        private void EnabledForm(bool p)
        {
            txtDiaChi.Enabled = p;
            txtDoanhSo.Enabled = p;
            //txtKyHieuHD.Enabled = p;
            txtMaSoThue.Enabled = p;
            txtNgayGD.Enabled = p;
            //txtNgayPS.Enabled = p;
            txtNoiDung.Enabled = p;
            txtSOBT.Enabled = p;
            //txtSoHD.Enabled = p;
            txtTen.Enabled = p;

            txtTK1.Enabled = p;
            txtTK2.Enabled = p;
            //txtTK3.Enabled = p;
            txtTKValue1.Enabled = p;
            txtTKValue2.Enabled = p;
            //txtTKValue3.Enabled = p;

            btnTK1.Enabled = p;
            btnTK2.Enabled = p;
            //btnTK3.Enabled = p;
            if (frmMain.__khoaSo)
            {
                btnSua.Enabled = btnXoa.Enabled = btnThem.Enabled = btnXoa.Enabled = false;
            }
            else
            {

                btnSua.Enabled = btnXoa.Enabled = btnThem.Enabled = btnXoa.Enabled = !p;
            }

            btnLuu.Enabled = p;
            btnBoQua.Enabled = p;
            btnTimKiem.Enabled = !p;
            btnIn.Enabled = !p;
            rChungTu.Enabled = !p;
            rLoai.Enabled = !p;
            string type = GetLoadBT();
            txtThueGTGT.Enabled = p & rLoai.EditValue.ToString() == "GTGT";
            btnTK3.Enabled = p & rLoai.EditValue.ToString() == "GTGT";
            txtTK3.Enabled = p & rLoai.EditValue.ToString() == "GTGT";
            txtTKValue3.Enabled = p & rLoai.EditValue.ToString() == "GTGT";
            txtNgayPS.Enabled = p & rLoai.EditValue.ToString() == "GTGT";
            txtSoHD.Enabled = p & rLoai.EditValue.ToString() == "GTGT";
            txtKyHieuHD.Enabled = p & rLoai.EditValue.ToString() == "GTGT";

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnabledForm(true);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (rLoai.EditValue.ToString() == "GTGT")
            {
                if (txtKyHieuHD.Text == "" || txtSoHD.Text == "")
                {
                    XtraMessageBox.Show("Vui lòng nhập thêm thông tin hóa đơn!");
                    return;
                }
            }
            BUS.BAOCAOTHUEControl cbCtr = new BUS.BAOCAOTHUEControl();
            DTO.BAOCAOTHUEInfo bcInf = GetData();
            if (bcInf.ID == -1)
            {
                bcInf.SOBT = NXHoaDon.GetNewSoBT();
                no = bcInf.ID = cbCtr.Add(bcInf, ref sErr);
                txtSOBT.Text = bcInf.SOBT;
            }
            else sErr = cbCtr.Update(bcInf);
            if (sErr == "")
            {
                BUS.HACHTOANControl htCtr = new BUS.HACHTOANControl();
                htCtr.Delete(bcInf.ID, ref sErr);
                string type = GetLoadBT();
                if (type == "C-GTGT")
                {
                    DTO.HACHTOANInfo htInf1 = new DTO.HACHTOANInfo(bcInf.ID, 1, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK1.Text, txtTK3.Text, Convert.ToDouble(txtTKValue1.EditValue), "");
                    DTO.HACHTOANInfo htInf2 = new DTO.HACHTOANInfo(bcInf.ID, 2, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK2.Text, txtTK3.Text, Convert.ToDouble(txtTKValue2.EditValue), "");
                    htCtr.Add(htInf1, ref sErr);
                    htCtr.Add(htInf2, ref sErr);
                }
                else if (type == "T-GTGT")
                {
                    DTO.HACHTOANInfo htInf1 = new DTO.HACHTOANInfo(bcInf.ID, 1, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK1.Text, txtTK2.Text, Convert.ToDouble(txtTKValue2.EditValue), "");
                    DTO.HACHTOANInfo htInf2 = new DTO.HACHTOANInfo(bcInf.ID, 2, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK1.Text, txtTK3.Text, Convert.ToDouble(txtTKValue3.EditValue), "");
                    htCtr.Add(htInf1, ref sErr);
                    htCtr.Add(htInf2, ref sErr);
                }
                else if (type == "TM-THU")
                {
                    DTO.HACHTOANInfo htInf1 = new DTO.HACHTOANInfo(bcInf.ID, 1, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK1.Text, txtTK2.Text, Convert.ToDouble(txtTKValue1.EditValue), "");
                    htCtr.Add(htInf1, ref sErr);
                }
                else
                {
                    DTO.HACHTOANInfo htInf1 = new DTO.HACHTOANInfo(bcInf.ID, 1, bcInf.SOBT, type, frmMain.__ngayMoSo, txtTK1.Text, txtTK2.Text, Convert.ToDouble(txtTKValue1.EditValue), "");
                    htCtr.Add(htInf1, ref sErr);
                }
                EnabledForm(false);
            }
            else XtraMessageBox.Show(sErr);
        }

        private DTO.BAOCAOTHUEInfo GetData()
        {
            DTO.BAOCAOTHUEInfo ret = new DTO.BAOCAOTHUEInfo();
            ret.ID = no;
            ret.BANRA = rChungTu.EditValue.ToString() == "IN";
            ret.DIACHI = txtDiaChi.Text;
            ret.DOANHSO = Convert.ToDouble(txtDoanhSo.EditValue);
            ret.GHICHU = ret.HANGHOA = txtNoiDung.Text;
            ret.ID = no;
            ret.KYHIEUHD = txtKyHieuHD.Text;
            ret.LOAIBT = GetLoadBT();
            ret.MSTHUE = txtMaSoThue.Text;
            ret.NGAYGD = txtNgayGD.EditValue != null ? Convert.ToDateTime(txtNgayGD.EditValue) : frmMain.__ngayMoSo;
            ret.NGAYPS = txtNgayPS.EditValue != null ? Convert.ToDateTime(txtNgayPS.EditValue) : frmMain.__ngayMoSo;
            ret.SOBT = txtSOBT.Text;
            ret.SOHD = txtSoHD.Text;
            ret.TENKH = txtTen.Text;
            ret.THUESUAT = Convert.ToInt16(txtThueGTGT.EditValue);
            ret.THUEGTGT = ret.DOANHSO * ret.THUESUAT / 100;
            ret.CREATEBY = frmMain.__userID;
            ret.MODIFIEDBY = frmMain.__userID;
            ret.CREATEDATE = DateTime.Now;
            ret.MODIFIEDDATE = DateTime.Now;
            return ret;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            EnabledForm(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (no != -1 && XtraMessageBox.Show("Bạn có thực sự muốn xóa?", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                BUS.BAOCAOTHUEControl bcCtr = new BUS.BAOCAOTHUEControl();
                sErr = bcCtr.Delete(no);
                if (sErr == "")
                {
                    BUS.HACHTOANControl htCtr = new BUS.HACHTOANControl();
                    htCtr.Delete(no, ref sErr);
                    EnabledForm(false);
                    RefreshForm("");
                }
                else XtraMessageBox.Show(sErr);
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            frmViewThuChiHD frm = new frmViewThuChiHD();
            frm.LoaiBT = GetLoadBT();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                DTO.BAOCAOTHUEInfo inf = new DTO.BAOCAOTHUEInfo(frm.ReturnRow);
                SetData(inf);
            }
        }

        private void SetData(DTO.BAOCAOTHUEInfo inf)
        {
            no = inf.ID;
            sobt = inf.SOBT;
            txtDiaChi.Text = inf.DIACHI;
            txtDoanhSo.EditValue = inf.DOANHSO + inf.THUEGTGT;
            txtKyHieuHD.Text = inf.KYHIEUHD;
            txtMaSoThue.Text = inf.MSTHUE;
            if (inf.TENKH == "")
            {
                if (inf.MSTHUE != "")
                {
                    txtTen.Text = NXHoaDon.GetMSTInfo(txtMaSoThue.Text);
                }
            }
            else
                txtTen.Text = inf.TENKH;
            txtNgayGD.EditValue = inf.NGAYGD;
            txtNgayPS.EditValue = inf.NGAYPS;
            txtNoiDung.Text = inf.HANGHOA;
            txtSOBT.Text = inf.SOBT;
            txtSoHD.Text = inf.SOHD;

            txtThueGTGT.EditValue = inf.THUESUAT;
            BUS.HACHTOANControl htCtr = new BUS.HACHTOANControl();
            DataTable dt = htCtr.GetByNo(inf.ID, ref sErr);
            string type = GetLoadBT();
            txtTK1.Text = txtTK2.Text = txtTK3.Text = "";
            txtTKValue1.EditValue = txtTKValue2.EditValue = txtTKValue3.EditValue = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DTO.HACHTOANInfo htInf = new DTO.HACHTOANInfo(dt.Rows[i]);
                if (type == "TM-THU")
                {
                    if (i == 0)
                    {
                        txtTK1.Text = htInf.TKNO;
                        txtTK2.Text = htInf.TKCO;
                        txtTKValue1.EditValue = txtTKValue2.EditValue = htInf.SOTIEN;
                    }
                }
                else if (type == "TM-CHI")
                {
                    if (i == 0)
                    {
                        txtTK1.Text = htInf.TKNO;
                        txtTK2.Text = htInf.TKCO;
                        txtTKValue1.EditValue = txtTKValue2.EditValue = htInf.SOTIEN;

                    }
                }
                else if (type == "T-GTGT")
                {
                    if (i == 0)
                    {
                        txtTK1.Text = htInf.TKNO;
                        txtTK2.Text = htInf.TKCO;
                        txtTKValue2.EditValue = htInf.SOTIEN;

                    }
                    if (i == 1)
                    {
                        txtTK1.Text = htInf.TKNO;
                        txtTK3.Text = htInf.TKCO;
                        txtTKValue3.EditValue = htInf.SOTIEN;
                        txtTKValue1.EditValue = Convert.ToDouble(txtTKValue2.EditValue) + htInf.SOTIEN;

                    }
                }
                else if (type == "C-GTGT")
                {
                    if (i == 0)
                    {
                        txtTK1.Text = htInf.TKNO;
                        txtTK3.Text = htInf.TKCO;
                        txtTKValue1.EditValue = htInf.SOTIEN;

                    }
                    if (i == 1)
                    {
                        txtTK2.Text = htInf.TKNO;
                        txtTK3.Text = htInf.TKCO;
                        txtTKValue2.EditValue = htInf.SOTIEN;
                        txtTKValue3.EditValue = Convert.ToDouble(txtTKValue1.EditValue) + htInf.SOTIEN;

                    }
                }
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            DataSet _ds = new DataSet();
            if (no == -1)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }

            string query = @"exec procBAOCAOTHUE_baocaothue @ID={0}";
            query = string.Format(query, no);
            BUS.CommonControl ctr = new BUS.CommonControl();
            _ds = ctr.executeSelectQueries(query);
            if (_ds.Tables.Count == 0)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            bool isdata = false;
            foreach (DataTable dt in _ds.Tables)
            {
                if (dt.Rows.Count > 0)
                {
                    isdata = true;
                    break;
                }
            }
            if (isdata == false)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            string reportCode = "PC_TM";
            if (rChungTu.EditValue.ToString() == "IN")
                reportCode = "PT_TM";
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = this.Parent.Parent as frmMain;
                main.ShowReport(_ds, reportCode, "Báo cáo thu chi hóa đơn");
            }

        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void rLoai_Properties_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = GetLoadBT();
            if (type == "T-GTGT")
            {
                lbTK1.Text = "TK Nợ";
                lbTK3.Text = lbTK2.Text = "TK Có";
            }
            else if (type == "C-GTGT")
            {
                lbTK1.Text = lbTK2.Text = "TK Nợ";
                lbTK3.Text = "TK Có";
            }
            else
            {
                lbTK1.Text = "TK Nợ";
                lbTK2.Text = "TK Có";
            }
        }

        private void rChungTu_Properties_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = GetLoadBT();
            if (type == "T-GTGT")
            {
                lbTK1.Text = "TK Nợ";
                lbTK3.Text = lbTK2.Text = "TK Có";
            }
            else if (type == "C-GTGT")
            {
                lbTK1.Text = lbTK2.Text = "TK Nợ";
                lbTK3.Text = "TK Có";
            }
            else
            {
                lbTK1.Text = "TK Nợ";
                lbTK2.Text = "TK Có";
            }
        }

        private void txtMaSoThue_Validating(object sender, CancelEventArgs e)
        {
            if (txtMaSoThue.Text != "")
            {
                string ten = "";
                ten = NXHoaDon.GetMSTInfo(txtMaSoThue.Text);

                if (ten == "")
                    e.Cancel = true;
                else txtTen.Text = ten;
            }
        }

        private void ThuChiHoaDon_Load(object sender, EventArgs e)
        {
            EnabledForm(false);
            SetPermission();
        }

        private void txtDoanhSo_Validated(object sender, EventArgs e)
        {
            string type = GetLoadBT();
            if (type == "TM-THU" || type == "TM-THU")
            {
                txtTKValue1.EditValue = txtTKValue2.EditValue = txtDoanhSo.EditValue;
            }
            else if (type == "T-GTGT")
            {

                double value2 = Math.Round(Convert.ToDouble(txtDoanhSo.EditValue) / 1.1, 0);
                txtTKValue2.EditValue = value2;
                txtTKValue3.EditValue = Convert.ToDouble(txtDoanhSo.EditValue) - value2;
                txtTKValue1.EditValue = txtDoanhSo.EditValue;
            }
            else
            {
                double value1 = Math.Round(Convert.ToDouble(txtDoanhSo.EditValue) / 1.1, 0);
                txtTKValue1.EditValue = value1;
                txtTKValue2.EditValue = Convert.ToDouble(txtDoanhSo.EditValue) - value1;
                txtTKValue3.EditValue = txtDoanhSo.EditValue;
            }
        }

        private void rChungTu_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshForm("");
        }

        private void ThuChiHoaDon_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);

            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);

            }
        }
    }
}